<template>
  <div class="main">
    <div class="word-order">
      <div class="title">上门拜访</div>
      <div class="row">
        <div class="row-input">
          <div class="lf">选择地区</div>
          <div class="md">:</div>
          <el-cascader size="large" :options="options" v-model="location">
          </el-cascader>
        </div>
        <div class="row-input">
          <div class="lf">时间段</div>
          <div class="md">:</div>
          <el-date-picker
            v-model="visit_time"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div>
      </div>
      <div class="row">
        <div class="row-input">
          <div class="lf">拜访事件</div>
          <div class="md">:</div>
          <el-select v-model="reason" placeholder="请选择">
            <el-option
              v-for="item in reasonOptions"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </div>
        <div class="row-input">
          <div class="lf">联系方式</div>
          <div class="md">:</div>
          <input type="text" maxlength="11" class="rt" v-model="phone" />
        </div>
      </div>
      <div class="row-textarea">
        <div class="lf">描述问题</div>
        <div class="md">:</div>
        <textarea
          v-model="issue"
          name=""
          id=""
          cols="30"
          rows="10"
          class="rt"
        ></textarea>
      </div>
      <!-- <div class="row-email">
        <div class="lf">邮箱验证</div>
        <div class="md">:</div>
        <input type="text" class="rt" v-model="email" />
        <span>获取验证码</span>
      </div> -->
      <div class="row-security-code">
        <div class="lf">验证码</div>
        <div class="md">:</div>
        <input type="text" class="rt" v-model="code" />
        <img
          :src="codeImg"
          alt=""
          srcset=""
          @click="refresh"
          class="code-img"
        />
      </div>
      <!-- <div class="row-email">
        <div class="lf">邮箱</div>
        <div class="md">:</div>
        <input type="text" class="rt" v-model="email" />
      </div> -->
      <div class="submit-btn" @click="submite">提交</div>
    </div>
  </div>
</template>

<script>
import { regionData, CodeToText } from "element-china-area-data";
export default {
  data() {
    return {
      reasonOptions:[],//拜访事件选项
      codeImg: "http://wudao.cqlink.club/captcha?", //验证码图片
      options: regionData, // 地区三级联动
      location: [], // 地址
      visit_time: null, // 拜访时间
      reason: "", // 拜访事件
      phone: "", // 联系方式
      issue: "", // 问题描述
      // email: "", // 邮箱
      code: "", //验证码
      //禁止选择当前日期之前的日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
    };
  },
  created () {
    this.$http.getShijian().then((res) => {
      if (res.data.code === 0) {
        this.reasonOptions = res.data.data;
      } else {
        this.$message.error('请求失败，请重试！')
      }
    })
  },
  methods: {
    // 验证码刷新
    refresh() {
      var num = Math.ceil(Math.random() * 10); //生成一个随机数（防止缓存）
      this.codeImg = this.codeImg + num;
    },
    submite() {
      this.location = this.location.map((item) => {
        let location = "";
        return (location += CodeToText[item]);
      });
      const isPhone = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
      const isEmail =
        /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (
        this.location[0] === undefined ||
        this.visit_time == null ||
        this.reason.trim().length <= 0 ||
        this.phone.trim().length <= 0 ||
        this.issue.trim().length <= 0
        // ||this.email.trim().length <= 0
      ) {
        this.$message.error("请完善所有信息后再提交！");
      } else if (!isPhone.test(this.phone.trim())) {
        this.$message.error("请输入正确的手机号码！");
      }
      // else if (!isEmail.test(this.email.trim())) {
      //   this.$message.error("请输入正确的邮箱！");
      // }
      else {
        const params = new FormData();
        params.append(
          "location",
          this.location[0] + " " + this.location[1] + " " + this.location[2]
        );
        params.append("visit_time", this.visit_time);
        params.append("reason", this.reason);
        params.append("phone", this.phone);
        params.append("issue", this.issue);
        // params.append("email", this.email);
        params.append("code", this.code);
        this.$http.postVisit(params).then((res) => {
          if (res.data.code === 0) {
            this.companyProfileData = res.data.data;
            this.$message({
              message: "提交成功！",
              type: "success",
            });
            this.location = [];
            this.visit_time = null;
            this.reason = "";
            this.phone = "";
            this.issue = "";
            // this.email = "";
            this.code = "";
            this.refresh();
          } else {
            this.$message.error(res.data.msg);
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.word-order {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;
  padding: 0 100px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  border-radius: 10px;
  padding-bottom: 108px;
  margin-bottom: 80px;
  .title {
    padding: 40px 0;
    text-align: center;
    font-size: 48px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
  }
  .row {
    display: flex;
    justify-content: space-between;
  }
  .row-input {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    width: 440px;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 320px;
      height: 33px;
      line-height: 33px;
      border: 1px solid #707070;
      border-radius: 4px;
      padding: 0 10px;
    }
  }
  .row-textarea {
    display: flex;
    margin-bottom: 30px;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 680px;
      height: 73px;
      border: 1px solid #707070;
      border-radius: 4px;
      resize: none;
      padding: 10px;
    }
  }
  .row-email,
  .row-security-code {
    display: flex;
    margin-bottom: 30px;
    align-items: center;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 200px;
      height: 33px;
      line-height: 33px;
      border: 1px solid #707070;
      border-radius: 4px;
      padding: 0 10px;
      margin-right: 10px;
      font-size: 16px;
    }
    .code-img {
      width: 120px;
      height: 50px;
    }
    span {
      font-size: 20px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 40px;
      color: #536786;
      text-decoration: underline;
      cursor: pointer;
    }
  }
  .submit-btn {
    width: 306px;
    height: 88px;
    background: #536786;
    border: 1px solid #707070;
    border-radius: 4px;
    font-size: 48px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 88px;
    color: #ffffff;
    text-align: center;
    margin: 0 auto;
    cursor: pointer;
  }
  // scss中修改element样式
  ::v-deep {
    .el-input--suffix .el-input__inner {
      width: 320px;
      height: 33px;
      line-height: 33px;
      border: 1px solid #707070;
      border-radius: 4px;
      font-size: 16px;
    }
    .el-input__suffix {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .el-input__icon {
      line-height: 33px;
    }
  }
}
</style>
